﻿// Decompiled with JetBrains decompiler
// Type: cXMLWizard.CUiMainwindow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.OLPEditor.cXMLWizard.Resources;
using LayersParser.Extensions;
using LayersParser.Interfaces;
using LayersParser.Objects;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml.Linq;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Olp;
using Tecnomatix.Engineering.Ui;


namespace cXMLWizard;

public class CUiMainwindow : TxForm
{
  private OLPConfiguration olpConfiguration;
  private bool isDisabled;
  private List<string> dialogs = new List<string>();
  private List<Param> parameters = new List<Param>();
  private List<Command> commands = new List<Command>();
  private List<IConfiguration> configurations = new List<IConfiguration>();
  private Param parameter;
  private Command command;
  private string path;
  private string filepath;
  private TxOlpControllerUtilities controllerUtilities = new TxOlpControllerUtilities();
  private TxRobot txRobot;
  private XDocument xDocument;
  private IContainer components;
  private ToolStrip toolStrip1;
  private ToolStripLabel toolStripLabel1;
  private ToolStripTextBox commandTextBox;
  private Button bCancel;
  private Button bOK;
  private Button bApply;
  private CUiLayerExpander uiLayer;
  private CUiLayerExpander simulationLayer;
  private CUiLayerExpander downloadLayer;

  public CUiMainwindow()
  {
    this.InitializeComponent();
    this.InitializeComponents();
  }

  public bool IsDisabled
  {
    get => this.isDisabled;
    set
    {
      this.isDisabled = value;
      this.bOK.Enabled = this.bApply.Enabled = !value;
    }
  }

  public OLPConfiguration OLPConfiguration
  {
    get => this.olpConfiguration;
    set => this.olpConfiguration = value;
  }

  public Param Parameter
  {
    get => this.parameter;
    set
    {
      this.parameter = value;
      this.parameters.ReplaceParameter(this.parameter);
      this.xDocument.GoToRoboticParams().Elements().Remove<XElement>();
      this.command.Node.Element((XName) "RoboticParamRef").Elements().Remove<XElement>();
      foreach (Param parameter in this.parameters)
      {
        this.xDocument.GoToRoboticParams().Add((object) parameter.Node);
        if (this.command.Params.ParToStr().Contains(parameter.Name))
          this.command.Node.Element((XName) "RoboticParamRef").Add((object) new XElement((XName) "Param", (object) parameter.Name));
      }
      this.uiLayer.Parameters = this.parameters;
      this.simulationLayer.Parameters = this.parameters;
      this.downloadLayer.Parameters = this.parameters;
    }
  }

  public CUiLayerExpander SimulationLayer
  {
    get => this.simulationLayer;
    set => this.simulationLayer = value;
  }

  public CUiLayerExpander UILayer
  {
    get => this.uiLayer;
    set => this.uiLayer = value;
  }

  public Command Command
  {
    get => this.command;
    set => this.command = value;
  }

  private void InitializeComponents()
  {
    TxSelection activeSelection = TxApplication.ActiveSelection;
    TxOLPUtilities txOlpUtilities = new TxOLPUtilities();
    this.txRobot = (TxRobot) activeSelection.GetLastPickedItem();
    if (this.controllerUtilities.GetCustomizationPath((ITxRobotController) this.txRobot.Controller) != null)
    {
      this.path = this.controllerUtilities.GetCustomizationPath((ITxRobotController) this.txRobot.Controller);
      this.filepath = this.controllerUtilities.GetCustomizationPath((ITxRobotController) this.txRobot.Controller) + "OlpConfiguration/UserDefined.xml";
    }
    else if (this.txRobot.Controller.Name == "default")
    {
      this.path = txOlpUtilities.GetOLPInstallationDirectory() + "Default";
      this.filepath = txOlpUtilities.GetOLPInstallationDirectory() + "Default/OlpConfiguration/UserDefined.xml";
    }
    else
    {
      this.path = txOlpUtilities.GetOLPInstallationDirectory() + this.txRobot.Controller.Name;
      this.filepath = $"{txOlpUtilities.GetOLPInstallationDirectory()}{this.txRobot.Controller.Name}/OlpConfiguration/UserDefined.xml";
    }
    this.CheckDirectories(this.path);
    if (!File.Exists(this.filepath))
    {
      this.olpConfiguration = new OLPConfiguration("UserDefined", this.txRobot.Controller.Name, ((TxRobotRRSController) this.txRobot.Controller).Version);
      try
      {
        this.olpConfiguration.XDocument.Save(this.filepath);
      }
      catch (Exception ex)
      {
        int num = (int) TxMessageBox.Show($"{ex.Message}\n\n{cXMLWizardStringTable.ACCESS_VIOLATION}", cXMLWizardStringTable.NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      }
      this.xDocument = this.olpConfiguration.XDocument;
    }
    else
    {
      this.xDocument = XDocument.Load(this.filepath);
      this.olpConfiguration = new OLPConfiguration(this.xDocument, ((TxRobotRRSController) this.txRobot.Controller).Version);
    }
    this.command = new Command();
    this.parameters = this.olpConfiguration.Params;
    this.commands = this.olpConfiguration.Commands;
    this.xDocument.GoToOlpCommands().Add((object) this.command.Node);
    this.uiLayer.LayerExpanderCtor(this.command, this.command.UILayer);
    this.simulationLayer.LayerExpanderCtor(this.command, this.command.SimulationLayer);
    this.downloadLayer.LayerExpanderCtor(this.command, this.command.DownloadLayer);
    this.uiLayer.TxRobot = this.simulationLayer.TxRobot = this.downloadLayer.TxRobot = this.txRobot;
    this.uiLayer.NameOfLayer = "UI";
    this.simulationLayer.NameOfLayer = "Simulation";
    this.downloadLayer.NameOfLayer = "Robot command (Download Layer) ";
    this.uiLayer.Visible = false;
    this.simulationLayer.Visible = false;
    this.uiLayer.IsExpanded = false;
    this.simulationLayer.IsExpanded = false;
    this.downloadLayer.IsExpanded = true;
    this.downloadLayer.Select();
    this.xDocument.GoToOlpDialogs().Add((object) this.command.Dialog.Node);
    this.uiLayer.Control = this.simulationLayer.Control = this.downloadLayer.Control = (object) this;
    this.SetDialogSize();
    this.SetParametersAndCommands();
    this.uiLayer.Parameters = this.simulationLayer.Parameters = this.downloadLayer.Parameters = this.parameters;
  }

  private void SetParametersAndCommands()
  {
    foreach (string directory in Directory.GetDirectories(this.path))
    {
      if (directory.Contains("OlpConfiguration"))
      {
        foreach (string file in Directory.GetFiles(directory))
        {
          if (file.Contains("xml") && !file.Contains("UserDefined"))
          {
            OLPConfiguration olpConfiguration = new OLPConfiguration(XDocument.Load(file), ((TxRobotRRSController) this.txRobot.Controller).Version)
            {
              FilePath = file
            };
            foreach (Command command in olpConfiguration.Commands)
            {
              this.commands.Add(command);
              this.dialogs.Add(command.Dialog.Title);
            }
            foreach (Param obj in olpConfiguration.Params)
              this.parameters.Add(obj);
            this.configurations.Add((IConfiguration) olpConfiguration);
          }
        }
      }
      if (directory.Contains("MotionConfiguration"))
      {
        foreach (string file in Directory.GetFiles(directory))
        {
          if (file.Contains("xml"))
          {
            MotionConfiguration motionConfiguration = new MotionConfiguration(XDocument.Load(file), ((TxRobotRRSController) this.txRobot.Controller).Version)
            {
              FilePath = file
            };
            foreach (Command process in motionConfiguration.Processes)
              this.dialogs.Add(process.Dialog.Title);
            foreach (Param obj in motionConfiguration.Params)
              this.parameters.Add(obj);
            this.configurations.Add((IConfiguration) motionConfiguration);
          }
        }
      }
    }
  }

  public TxRobot TxRobot
  {
    get => this.txRobot;
    set => this.txRobot = value;
  }

  private void XMLCheck()
  {
    try
    {
      new TxOlpCommandServices(this.txRobot).Refresh();
    }
    catch (Exception ex)
    {
      if (ex is TxOlpXMLMissingOlpConfigurationFileException)
      {
        int num1 = (int) TxMessageBox.Show(cXMLWizardStringTable.CUSTOMIZED_OLP_MISSING_CONFIGURATION_FILE, cXMLWizardStringTable.CUSTOMIZED_OLP_COMMAND_CHECKER, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      }
      else
      {
        int num2 = (int) TxMessageBox.Show(ex.Message, cXMLWizardStringTable.CUSTOMIZED_OLP_COMMAND_CHECKER, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      }
    }
  }

  private void CloseWindow(object sender, EventArgs e)
  {
    if (!this.SaveConfiguration())
      return;
    ((Form) this).Close();
  }

  private bool SaveConfiguration()
  {
    bool flag = false;
    if (this.UnitarityTest())
    {
      try
      {
        this.olpConfiguration.XDocument.Save(this.filepath);
      }
      catch (Exception ex)
      {
        int num = (int) TxMessageBox.Show($"{ex.Message}\n\n{cXMLWizardStringTable.ACCESS_VIOLATION}", cXMLWizardStringTable.NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
        return false;
      }
      foreach (IConfiguration configuration in this.configurations)
      {
        try
        {
          configuration.XDocument.Save(configuration.FilePath);
        }
        catch (Exception ex)
        {
          int num = (int) TxMessageBox.Show($"{ex.Message}\n\n{cXMLWizardStringTable.ACCESS_VIOLATION}", cXMLWizardStringTable.NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
          return false;
        }
      }
      this.XMLCheck();
      flag = true;
    }
    return flag;
  }

  private void ApplyChange(object sender, EventArgs e) => this.SaveConfiguration();

  private void CancelWindow(object sender, EventArgs e) => ((Form) this).Close();

  private void NameChanged(object sender, EventArgs e)
  {
    this.command.Name = this.commandTextBox.Text;
    this.IsDisabled = this.commandTextBox.Text.Length == 0 || this.uiLayer.RichTextBox.Text.Length == 0 || this.downloadLayer.RichTextBox.Text.Length == 0;
  }

  private void ClickOnLayer(object sender, EventArgs e) => this.SetDialogSize();

  private void SetDialogSize()
  {
    this.downloadLayer.Size = new Size(((Form) this).Size.Width - 20, ((Form) this).Size.Height - 100);
    Button bCancel = this.bCancel;
    Size size = ((Form) this).Size;
    int x1 = size.Width - 100;
    size = this.downloadLayer.Size;
    int y1 = size.Height + 30;
    Point point1 = new Point(x1, y1);
    bCancel.Location = point1;
    Button bOk = this.bOK;
    Point location1 = this.bApply.Location;
    int x2 = location1.X - 80 /*0x50*/;
    location1 = this.bApply.Location;
    int y2 = location1.Y;
    Point point2 = new Point(x2, y2);
    bOk.Location = point2;
    Button bApply = this.bApply;
    Point location2 = this.bCancel.Location;
    int x3 = location2.X - 80 /*0x50*/;
    location2 = this.bCancel.Location;
    int y3 = location2.Y;
    Point point3 = new Point(x3, y3);
    bApply.Location = point3;
  }

  private bool UnitarityTest()
  {
    foreach (Command command in this.commands)
    {
      if (command.Name == this.command.Name)
      {
        if (TxMessageBox.Show(cXMLWizardStringTable.ALL_READY_EXIST, cXMLWizardStringTable.FORM_CLOSING, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation) == DialogResult.Yes)
        {
          command.Node.Remove();
          command.Dialog.Node.Remove();
          return true;
        }
        this.command.Node.Remove();
        this.command.Dialog.Node.Remove();
        return false;
      }
    }
    return true;
  }

  private bool CreateConfigurationDirectory(string filePath)
  {
    bool configurationDirectory = true;
    try
    {
      Directory.CreateDirectory(filePath);
    }
    catch (Exception ex)
    {
      int num = (int) TxMessageBox.Show($"{ex.Message}\n\n{cXMLWizardStringTable.ACCESS_VIOLATION}", cXMLWizardStringTable.NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
      configurationDirectory = false;
    }
    return configurationDirectory;
  }

  private bool CheckDirectories(string filePath)
  {
    return this.CreateConfigurationDirectory(filePath + "\\OlpConfiguration") && this.CreateConfigurationDirectory(filePath + "\\MotionConfiguration") && this.CreateConfigurationDirectory(filePath + "\\DataConfiguration") && this.CreateConfigurationDirectory(filePath + "\\DownloadTemplatesConfiguration") && this.CreateConfigurationDirectory(filePath + "\\PathTemplateConfiguration");
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiMainwindow));
    this.toolStrip1 = new ToolStrip();
    this.toolStripLabel1 = new ToolStripLabel();
    this.commandTextBox = new ToolStripTextBox();
    this.bCancel = new Button();
    this.bOK = new Button();
    this.bApply = new Button();
    this.uiLayer = new CUiLayerExpander();
    this.simulationLayer = new CUiLayerExpander();
    this.downloadLayer = new CUiLayerExpander();
    this.toolStrip1.SuspendLayout();
    ((Control) this).SuspendLayout();
    this.toolStrip1.GripStyle = ToolStripGripStyle.Hidden;
    this.toolStrip1.Items.AddRange(new ToolStripItem[2]
    {
      (ToolStripItem) this.toolStripLabel1,
      (ToolStripItem) this.commandTextBox
    });
    this.toolStrip1.Location = new Point(0, 0);
    this.toolStrip1.Name = "toolStrip1";
    this.toolStrip1.Padding = new Padding(1, 2, 1, 0);
    this.toolStrip1.Size = new Size(704, 25);
    this.toolStrip1.TabIndex = 0;
    this.toolStrip1.Text = "toolStrip1";
    this.toolStripLabel1.Name = "toolStripLabel1";
    this.toolStripLabel1.Size = new Size(39, 22);
    this.toolStripLabel1.Text = cXMLWizardStringTable.NAME_LABEL;
    this.toolStripLabel1.Padding = new Padding(1, 0, 0, 0);
    this.commandTextBox.Name = "commandTextBox";
    this.commandTextBox.Size = new Size(240 /*0xF0*/, 25);
    this.commandTextBox.TextChanged += new EventHandler(this.NameChanged);
    this.bCancel.Location = new Point(460, 630);
    this.bCancel.Name = "bCancel";
    this.bCancel.Size = new Size(75, 23);
    this.bCancel.TabIndex = 4;
    this.bCancel.Text = cXMLWizardStringTable.CANCEL_LABEL;
    this.bCancel.UseVisualStyleBackColor = true;
    this.bCancel.Click += new EventHandler(this.CancelWindow);
    this.bOK.Enabled = false;
    this.bOK.Location = new Point(620, 630);
    this.bOK.Name = "bOK";
    this.bOK.Size = new Size(75, 23);
    this.bOK.TabIndex = 5;
    this.bOK.Text = "OK";
    this.bOK.UseVisualStyleBackColor = true;
    this.bOK.Click += new EventHandler(this.CloseWindow);
    this.bApply.Enabled = false;
    this.bApply.Location = new Point(540, 630);
    this.bApply.Name = "bApply";
    this.bApply.Size = new Size(75, 23);
    this.bApply.TabIndex = 6;
    this.bApply.Text = cXMLWizardStringTable.APPLY_LABEL;
    this.bApply.UseVisualStyleBackColor = true;
    this.bApply.Click += new EventHandler(this.ApplyChange);
    this.downloadLayer.Control = (object) null;
    this.downloadLayer.IsExpanded = false;
    this.downloadLayer.Location = new Point(2, 30);
    this.downloadLayer.Name = "downloadLayer";
    this.downloadLayer.Size = new Size(700, 200);
    this.downloadLayer.TabIndex = 7;
    this.downloadLayer.SizeChanged += new EventHandler(this.ClickOnLayer);
    this.simulationLayer.Control = (object) null;
    this.simulationLayer.IsExpanded = false;
    this.simulationLayer.Location = new Point(2, 230);
    this.simulationLayer.Name = "simulationLayer";
    this.simulationLayer.Size = new Size(700, 200);
    this.simulationLayer.TabIndex = 8;
    this.simulationLayer.SizeChanged += new EventHandler(this.ClickOnLayer);
    this.uiLayer.Control = (object) null;
    this.uiLayer.IsExpanded = false;
    this.uiLayer.Location = new Point(2, 430);
    this.uiLayer.Name = "uiLayer";
    this.uiLayer.Size = new Size(700, 200);
    this.uiLayer.TabIndex = 9;
    this.uiLayer.SizeChanged += new EventHandler(this.ClickOnLayer);
    ((Form) this).ClientSize = new Size(300, 300);
    ((Control) this).Controls.Add((Control) this.bApply);
    ((Control) this).Controls.Add((Control) this.bOK);
    ((Control) this).Controls.Add((Control) this.bCancel);
    ((Control) this).Controls.Add((Control) this.toolStrip1);
    ((Control) this).Controls.Add((Control) this.uiLayer);
    ((Control) this).Controls.Add((Control) this.simulationLayer);
    ((Control) this).Controls.Add((Control) this.downloadLayer);
    ((Form) this).MaximizeBox = false;
    ((Control) this).MaximumSize = new Size(600, 600);
    ((Form) this).MinimizeBox = false;
    ((Control) this).MinimumSize = new Size(300, 400);
    ((Control) this).Name = "Mainwindow";
    ((Form) this).SizeGripStyle = SizeGripStyle.Hide;
    ((Control) this).Text = cXMLWizardStringTable.NAME;
    ((Control) this).SizeChanged += new EventHandler(this.ClickOnLayer);
    this.toolStrip1.ResumeLayout(false);
    this.toolStrip1.PerformLayout();
    ((Control) this).ResumeLayout(false);
    ((Control) this).PerformLayout();
  }
}
